fix: reject I/O operations on closed mocked filehandles#344
Draft
toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
Draft
fix: reject I/O operations on closed mocked filehandles#344toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
toddr-bot wants to merge 2 commits intocpan-authors:mainfrom
Conversation
…ctly After close($fh), operations like print, read, readline, seek, tell, and getc still succeed on mocked filehandles — real Perl would warn and return failure values (undef, -1, false). This test documents the bug: all 9 subtests fail against the current code, proving that closed-handle detection is missing from the tied FileHandle implementation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
After close($fh), operations like print, read, readline, syswrite,
seek, tell, getc, and eof on mocked filehandles silently succeeded —
writing data, reading stale contents, and reporting incorrect positions.
Real Perl rejects these operations with warnings ("op() on closed
filehandle") and appropriate return values (undef, -1, false).
The fix tracks closed state in CLOSE and checks it at the top of each
I/O method, matching real Perl's return values and warning format:
- print/printf/syswrite/read/readline/getc: return undef + warn
- tell: return -1 + warn
- seek: return false + warn
- eof: return true (no warning, matches real Perl)
- double close: return false with EBADF
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
I/O operations on a closed mocked filehandle now fail with proper warnings and return values, matching real Perl behavior.
Why
After
close($fh), operations likeprint,read,readline,seek,tell, andgetcsilently succeeded on mocked filehandles — writing data to the mock, reading stale contents, and reporting incorrect positions. Real Perl rejects all of these with warnings and appropriate failure returns. This discrepancy means tests using Test::MockFile could pass even when application code incorrectly operates on closed handles.How
closedflag to the tied FileHandle, set inCLOSE_is_closed_with_warning()helper that emits Perl-style"op() on closed filehandle"warningsclose()returns false with$! = EBADFTesting
t/closed_fh_ops.twith 9 subtests covering all affected operationsfh-ref-leak.tfailure)🤖 Generated with Claude Code
Quality Report
Changes: 2 files changed, 188 insertions(+)
Code scan: clean
Tests: failed (4 Failed, 95 test)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline